package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.UserSoulReport;
import com.tanhua.dubbo.api.UserSoulReportApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserSoulReportApiImpl implements UserSoulReportApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public UserSoulReport findByUid(Long userId) {
        Query query = new Query(Criteria.where("uid").is(userId));
        UserSoulReport userSoulReport = mongoTemplate.findOne(query, UserSoulReport.class);
        return userSoulReport;
    }

    @Override
    public void updateUserReport(Long userId, Integer star, String text) {
        String field = null;
        switch (star) {
            case 2:
                field = "testReport1";
                break;
            case 3:
                field = "testReport2";
                break;
            case 5:
                field = "testReport3";
                break;
        }

        Query query = new Query(Criteria.where("uid").is(userId));
        Update update = new Update();
        update.set(field, text);

        mongoTemplate.updateFirst(query, update, UserSoulReport.class);
    }

    @Override
    public List<Long> findUserByConclusion(UserSoulReport userSoulReport) {

        Query query = new Query(Criteria.where("testReport1").is(userSoulReport.getTestReport1()));
        query.limit(10);
        List<UserSoulReport> userSoulReports = mongoTemplate.find(query, UserSoulReport.class);
        List<Long> userIdList = new ArrayList<>();
        if (userSoulReports!=null){
        for (UserSoulReport soulReport : userSoulReports) {
            userIdList.add(soulReport.getUid());
        }
        }

        return userIdList;
    }
}
